﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace quizzer_linq_3
{
    /**Class: Reset_Password
     * @author Samuel McCravy
     * @version 1.1
     * Course : ITEC 4860 Spring 2012
     * Written: March 22, 2012
     * 
     * This Class allows admins to change anyones password
     * 
     * Purpose: The purpose of this class is to allow admins to change anyones password
    */

    public partial class Reset_Password : Form
    {
        //This is a constant used to lookup the professor's ID

        const int PROFESSOR_ID_LOOKUP_INDEX = 0;

        //This is a constant used to lookup the professor's first name

        const int PROFESSOR_FIRST_NAME_LOOKUP_INDEX = 1;

        //This is a constant used to lookup the professor's last name

        const int PROFESSOR_LAST_NAME_LOOKUP_INDEX = 2;

        //This is a constant used to lookup professor's email for resetting his password

        const int PROFESSOR_EMAIL_LOOKUP_INDEX = 3;

        public Reset_Password()
        {
            InitializeComponent();
        }

        /** Method: backButton_Click
         * Closes this form
         * 
         * @param sender sender of the action
         * @param e event is triggered
        */

        private void backButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /** Method: resetButton_Click
         * Allows the reset of any users password
         * 
         * @param sender sender of the action
         * @param e event is triggered
        */

        private void resetButton_Click(object sender, EventArgs e)
        {

            //newPassword gets the new password from the textbox on the form

            string newPassword = (string)textBoxNewPassword.Text;

            //Email gets the email from the textbox

            string email = (string)textBoxResetEmail.Text; ;

            //This checks to see if the fields have values and then changes the correct professor's password in the database.

            if (newPassword.Length > 0 && email.Length > 0)
            {
                try
                {
                    //DataAccess is the variable for using the database

                    Quizzer_Data_Access dataAccess = new Quizzer_Data_Access();

                    //Gets the professor from the database and puts his information into an array

                    String[] getAllProfessor = dataAccess.GetAllProfessor().Split('\n');

                    //Checks for the email in the database and then uses it to reset his password

                    for (int counter = 0; counter < getAllProfessor.Length - 1; counter++)
                    {
                        String[] checkProfessor = ((String)getAllProfessor.GetValue(counter)).Split('|');
                        if (email.Equals((String)checkProfessor.GetValue(PROFESSOR_EMAIL_LOOKUP_INDEX)))
                        {
                            DialogResult dialogResult = MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButtons.YesNo);
                            if (dialogResult == DialogResult.Yes)
                            {
                                dataAccess.UpdateProfessor(int.Parse(checkProfessor.GetValue(PROFESSOR_ID_LOOKUP_INDEX).ToString()), (String)checkProfessor.GetValue(PROFESSOR_FIRST_NAME_LOOKUP_INDEX), (String)checkProfessor.GetValue(PROFESSOR_LAST_NAME_LOOKUP_INDEX), email, newPassword);
                                MessageBox.Show("Password Changed", "Successful Update");
                                this.Close();
                            }
                            
                        }
                        
                    }
                }
                catch (Exception caught)
                {
                    MessageBox.Show("Password updating Error has Occurred, Please make sure all fields are filled out correctly.", "Error!");
                    System.Diagnostics.EventLog.WriteEntry("Database rewrite/search failed.", caught.StackTrace, System.Diagnostics.EventLogEntryType.Warning);
                }
            }
            else
            {
                MessageBox.Show("Fields Empty, Please make sure all fields have the correct information", "Error!");
            }
        }
    }
}
